angular.module('charity.auditInfo', [])
    .controller('auditCtr', ['$scope', '$filter', 'auditService', '$routeParams', '$uibModal','logger',
        function ($scope, $filter, auditService, $routeParams, $uibModal, logger) {
            $scope.stores = [];
            $scope.searchKeywords = '';
            $scope.filteredStores = [];
            $scope.row = '',

                $scope.selectedMenu = {};
            $scope.menus = [];

            $scope.select = function (page) {
                var end, start;
                start = (page - 1) * $scope.numPerPage;
                end = start + $scope.numPerPage;
                return $scope.currentPageStores = $scope.filteredStores.slice(start, end);
            };

            $scope.onFilterChange = function () {
                $scope.select(1);
                $scope.currentPage = 1;
                return $scope.row = '';
            };

            $scope.onNumPerPageChange = function () {
                $scope.select(1);
                return $scope.currentPage = 1;
            };

            $scope.onOrderChange = function () {
                $scope.select(1);
                return $scope.currentPage = 1;
            };

            $scope.search = function () {
                auditService.queryAllAudits($scope.selectedMenu.menuId).then(function (results) {

                    if (!results || results.code != 0) {
                        logger.logError((results && results.message) || '查询数据失败,请重新登录');
                        return;
                    }
                    $scope.stores = results.data || [];

                    $scope.filteredStores = $filter('filter')($scope.stores, $scope.searchKeywords);
                    return $scope.onFilterChange();
                });
            };

            $scope.order = function (rowName) {
                if ($scope.row === rowName) {
                    return;
                }
                $scope.row = rowName;
                $scope.filteredStores = $filter('orderBy')($scope.stores, rowName);
                return $scope.onOrderChange();
            };
            $scope.numPerPageOpt = [3, 5, 10, 20];
            $scope.numPerPage = $scope.numPerPageOpt[2];
            $scope.currentPage = 1;
            $scope.currentPageStores = [];

            $scope.deleteAudit = function (audit) {
                var modalInstance = $uibModal.open({
                    animation: true,
                    templateUrl: 'deleteModalContent.html',
                    controller: 'deleteComponentCtrl',
                    resolve: {
                        data: function () {
                            return {id: audit.id, name: audit.name}
                        }
                    }
                });

                modalInstance.result.then(function (auditId) {
                    auditService.deleteAudit(auditId).then(function (results) {
                        if (results && results.code == 0) {
                            logger.logSuccess('操作成功');
                            $scope.search();

                        }
                        else {
                            logger.logError((results && results.message) || '操作失败,请重新登录');
                        }

                    })
                })
            };

            $scope.search();
        }])

    .controller('editAuditCtrl', ['$scope', '$commonService', 'auditService', '$routeParams', 'logger', 'cfpLoadingBar', '$location',
        function ($scope, $commonService, auditService, $routeParams, logger, cfpLoadingBar, $location) {
            $scope.auditId = $routeParams.auditId;
            if (!!$scope.auditId) {
                auditService.getAudit($scope.auditId).then(function (results) {
                    if (!results || results.code != 0) {
                        logger.logError((results && results.message) || '查询数据失败,请重新登录');
                        return;
                    }
                    $scope.audit = results.data;

                })
            }
            else {
                $scope.audit = {type: 'article', stick: 'N'};

            }


            $scope.imgUploadCallback = function () {
                $scope.$apply();
            };

            $scope.saveAudit = function () {

                if (!!$scope.audit.id) {
                    auditService.modifyAudit($scope.audit).then(function (results) {
                        if (!results || results.code != 0) {
                            logger.logError((results && results.message) || '操作数据失败,请重新登录');
                            return;
                        }
                        logger.logSuccess('保存成功');

                    });
                }
                else {
                    auditService.addAudit($scope.audit).then(function (results) {
                        if (!results || results.code != 0) {
                            logger.logError((results && results.message) || '操作数据失败,请重新登录');
                            return;
                        }
                        logger.logSuccess('保存成功');
                        debugger;
                        $location.path('/audits/edit/' + results.data.id);
                    })
                }

            }


        }])
    .controller('deleteComponentCtrl', ['$scope', '$uibModalInstance', 'data',
        function ($scope, $uibModalInstance, data) {
            $scope.data = data;
            $scope.ok = function () {
                $uibModalInstance.close($scope.data.id);
            };
            $scope.cancel = function () {
                $uibModalInstance.dismiss("cancel");
            };

        }])
    .factory('auditService', ['$http', function ($http) {
        return {
            queryAllAudits: function () {
                return $http({
                    method: "GET",
                    url: "/management/audits"
                }).then(function (results) {
                    return results.data;
                })
            },
            getAudit: function (auditId) {
                return $http({
                    method: "GET",
                    url: "/management/audits/" + auditId,
                }).then(function (results) {
                    return results.data;
                })
            },
            addAudit: function (audit) {
                return $http({
                    method: "POST",
                    url: "/management/audits/",
                    data: audit
                }).then(function (results) {
                    return results.data;
                })
            },
            modifyAudit: function (audit) {
                return $http({
                    method: "PUT",
                    url: "/management/audits/" + audit.id,
                    data: audit
                }).then(function (results) {
                    return results.data;
                })
            },

            deleteAudit: function (auditId) {
                return $http({
                    method: "DELETE",
                    url: "/management/audits/" + auditId
                }).then(function (results) {
                    return results.data;
                })
            }
        }
    }]);